<?php
/**
 * 砍价活动订单Model
 * User: Administrator
 * Date: 2017/7/14
 * Time: 17:03
 */
defined('InNcPHP') or exit('Access Invalid!');

class  bargain_orderModel extends Model{
    public $redis;

    public $deploy;
    public $status = array('10'=>'待支付', '20'=>'已支付','21'=>'已核销','40'=>'已发货','41'=>'已收货');
    public $type = array('0'=>'物流发货','1'=>'实体消费');
    public  function  __construct(){
        $this->redis = new Redis();
        $this->deploy = C('redis');
        $this->redis->connect($this->deploy['master']['host'],$this->deploy['master']['port']);
        parent::__construct('bargain_order');
    }

    /**
     * 获取活动订单列表分页
     * @param array $condition
     * @param string $field
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public function getBgOrderListPage($condition = array(), $field = '*', $page = 0, $order = 'bg_order_id desc'){
        return $this->field($field)->where($condition)->page($page)->order($order)->select();
    }

    /**
     * 获取一条记录的详细信息
     * @param array $condition
     * @param string $field
     * @return mixed
     */
    public function getBgOrderInfo($condition = array(), $field = '*')
    {
        return $this->field($field)->where($condition)->find();
    }

    /**
     * 获取砍赞总次数最高的记录
     * @param array $condition
     * @param string $field
     * @param string $order
     */
    public function getBgordermax($condition = array(),$field = '*',$order = 'in_all desc ,add_time asc'){
        return $this->field($field)->where($condition)->order($order)->find();
    }

    /**
     * 插入一条记录
     * @param array $params
     * @return mixed
     */
    public function addBgOrder($params)
    {
        return $this->insert($params);
    }


    /**
     * 更新记录
     * @param array $condition
     * @param array $params
     * @return mixed
     */
    public function updateBgOrder($condition, $params)
    {
        return $this->where($condition)->update($params);
    }


    /**
     * 取得订单数量
     * @param array $condition
     */
    public function getBgOrderCount($condition) {
        return $this->table('bargain_order')->where($condition)->count();
    }

    /**
     * 检查该订单是否属于自己
     *@param  $order_id
     * @param  $member_id
     * @param    $bargain_id
     */

    public function Checkismeorder($order_id,$member_id,$bargain_id){
        $condition = array();
        $condition['member_id']   = $member_id;
        $condition['bg_order_id'] = $order_id;
        $condition['bargain_id']  = $bargain_id;
        $data = $this->table('bargain_order')->where($condition)->find();
        if(!empty($data)){
            return true;
        }else {
            return false;
        }

    }

    /**
     * 检查活动懂订单是否属于爆款
     * @param    $bargain_id
     */

    private function bargain_is_hot($bargain_id){

        return $this->table('bargain')->where(array('bargain_id'=>$bargain_id))->find();

    }

    /**
     * 统计爆款活动购买资格的人数
     * @param    $bargain_id
     */
    private function  true_hot_number($bargain_id){

        $condition = array();
        $condition['bargain_id'] = $bargain_id;
        $condition['min_time'] = array('neq',null);
        return $this->where($condition)->count();

    }

    /**
     * 砍价成功改变订单价格
     */
    public function updateorder_price($order_id,$price,$member_id,$nickname='',$headimgurl=''){

        $order_name = $this->deploy['prefix'].'orders'.$order_id;
        $monthordername= $this->deploy['prefix'].'month_order'.$order_id;
        $order_sum_name = $this->deploy['prefix'].'orderu-'.$order_id.'-'.$member_id;
        $pxname = $this->deploy['prefix'].'orderpx';
        //$res = $this->getBgOrderInfo(array('bg_order_id'=>$order_id),'bg_price,bg_order_price,bg_floorprice,bargain_id');
        $res = $this->redis->hGetAll($order_name);//获取订单信息

        if(empty($price)){
              //金额为空,砍价失败写入砍价次数表
               //$this->table('bargain_count')->where(array('member_id'=>$member_id,'bg_order_id'=>$order_id))->setInc('in_all');
               //$this->table('bargain_order')->where(array('bg_order_id'=>$order_id))->setInc('in_all');
              $this->redis->hIncrBy($order_name,'in_all',1);
              $this->redis->hIncrBy($monthordername,'in_all',1);
              $this->redis->hIncrBy($order_sum_name,'in_all',1);
              $this->redis->zIncrBy($pxname,1,'order_id-'.$order_id);//redis 活动PK榜单当前订单砍赞次数加一
              return;
          }
           //判断是否砍到最低价格
        if($res['bg_order_price']>$res['bg_floorprice']){

               if(($res['bg_order_price']-$res['bg_floorprice'])>$price){
                   $order_price = $price;
               }else{
                   //说明砍到最低价
                   $order_price = $res['bg_order_price']-$res['bg_floorprice'];
                   $file = BASE_DATA_PATH.'/lock/'.$res['bargain_id'];
                   $name = $this->deploy['prefix'].'bargain_'.$res['bargain_id'];
                   $is_hot = json_decode($this->redis->get($name),true);
                   if($is_hot['is_hot']==1){  //该活动属于爆款

                       if ($fp = fopen($file, 'w')) {
                           flock($fp, LOCK_EX);//加排它锁锁
                           //执行你的代码
                           $true_number = $this->true_hot_number($res['bargain_id']);
                           if($true_number < $is_hot['bg_store']){//判断是否被抢光
                               //没有被抢光写入砍到最低价时间
                               $this->table('bargain_order')->where(array('bg_order_id'=>$order_id,'bargain_id'=>$res['bargain_id']))->update(array('min_time'=>time()));
                               $this->table('bargain')->where(array('bargain_id'=>$res['bargain_id']))->setInc('bg_true_rs');
                               $this->table('bargain')->where(array('bargain_id'=>$res['bargain_id']))->setDec('bg_store');
                           }
                           flock($fp, LOCK_UN);//解锁
                           fclose($fp);
                       }
                  }


               }
               $res['bg_order_price'] = $res['bg_order_price']-$order_price;
               $this->table('bargain_order')->where(array('bg_order_id'=>$order_id))->update(array('bg_order_price'=>$res['bg_order_price']));//更新订单的实际价格
               //缓存中实际订单价格减去
               //$this->redis->hIncrByFloat($order_name,'bg_order_price',-$order_price);//服务器上打开
               //$this->redis->hIncrByFloat($order_sum_name,'cuont_sunm',$order_price);//增加砍价总成功金额
                $data = $this->redis->hGet($order_sum_name,'cuont_sunm');
                $this->redis->hSet($order_sum_name,'cuont_sunm',$data+$order_price);
                $this->redis->hSet($order_name,'bg_order_price',$res['bg_order_price']);
               //如果砍价金额不为0说明砍价成功,写入砍价成功表

               /*if($order_price>0){
                   $count_info  = array();
                   $count_info['member_id']  = $member_id;
                   $count_info['add_time']   = TIMESTAMP;
                   $count_info['bg_order_id']= $order_id;
                   $count_info['true_sum']   = $order_price;
                   $this->table('bargain_count_sum')->insert($count_info);
               }*/
               //查询砍价总次数
               //$count = $this->table('bargain_count')->where(array('member_id'=>$member_id,'bg_order_id'=>$order_id))->find();
               $count = $this->redis->hGetAll($order_sum_name);

               if($count['member_id'] ==$member_id){
                  //更新
                   $true_count = array();
                   if($order_price>0){
                       $this->redis->hIncrBy($order_sum_name,'true_all',1);//缓存砍价成功次数加一
                       $true_count['cuont_sunm'] = $count['cuont_sunm'];
                   }

                   $this->table('bargain_count')->where(array('member_id'=>$member_id,'bg_order_id'=>$order_id))->update($true_count);//修改数据库砍价次数中砍成功的价格
                   $this->redis->hIncrBy($order_name,'in_all',1);//redis 活动订单砍价总次数加一
                   $this->redis->hIncrBy($monthordername,'in_all',1);//当月的活动订单砍价总次数加一
                   $this->redis->zIncrBy($pxname,1,'order_id-'.$order_id);//redis 活动PK榜单当前订单砍赞次数加一
                   $this->redis->hIncrBy($order_sum_name,'in_all',1);//redis 缓存砍价总次数加一



               }else{
                   //新插入
                   $true_count = array();
                   $true_count['member_id'] = $member_id;
                   $true_count['nickname']  = $nickname;
                   $true_count['headimgurl']= $headimgurl;
                   $true_count['in_all'] = 1;
                   if($order_price>0){
                       $true_count['true_all'] = 1;
                   }else{
                       $true_count['true_all'] = 0;
                   }
                   $true_count['bg_order_id'] = $order_id;
                   $true_count['add_time']    = TIMESTAMP;
                   $true_count['cuont_sunm']  = $order_price;
                   $datas = $this->table('bargain_count')->where(array('bg_order_id'=>$order_id,'member_id'=>$member_id))->find();
                   if(empty($datas)){
                       $this->table('bargain_count')->insert($true_count);
                   }else{
                       $this->table('bargain_count')->where(array('bg_order_id'=>$order_id,'member_id'=>$member_id))->update($true_count);
                   }

                   //砍价次数表写入redis缓存
                   $this->redis->hSet($order_sum_name,'member_id',$member_id);//砍价者的用户ID
                   $this->redis->hSet($order_sum_name,'headimgurl',$headimgurl);//用户头像
                   $this->redis->hSet($order_sum_name,'nickname',$nickname);//用户昵称
                   $this->redis->hSet($order_sum_name,'ba_order_id',$order_id);//被砍价订单ID
                   $this->redis->hSet($order_sum_name,'in_all',1);//砍价总次数
                   $this->redis->hSet($order_sum_name,'cuont_sunm',$order_price);//砍成功的金额
                   $this->redis->hSet($order_sum_name,'true_all',$true_count['true_all']);//砍价成功次数
                   $this->redis->hIncrBy($order_name,'in_all',1);//redis 活动订单砍价总次数加一
                   $this->redis->hIncrBy($monthordername,'in_all',1);//当月的活动订单砍价总次数加一
                   $this->redis->zIncrBy($pxname,1,'order_id-'.$order_id);//redis 活动PK榜单当前订单砍赞次数加一
               }


           } else {
               //表示已经砍到最低价 改变次数不改变价格
               //$this->table('bargain_count')->where(array('member_id'=>$member_id,'bg_order_id'=>$order_id))->setInc('in_all');
               //$this->table('bargain_order')->where(array('bg_order_id'=>$order_id))->setInc('in_all');
               $count = $this->redis->hGetAll($order_sum_name);
               if($count['member_id'] ==$member_id){
                   //该用户是老用户
                   $this->redis->hIncrBy($order_name,'in_all',1);//改变缓存
                   $this->redis->hIncrBy($order_sum_name,'in_all',1);//改变缓存
                   $this->redis->hIncrBy($monthordername,'in_all',1);//当月的活动订单砍价总次数加一
                   $this->redis->zIncrBy($pxname,1,'order_id-'.$order_id);//redis 活动PK榜单当前订单砍赞次数加一
               }else{
                   //用户是新用户
                   $this->redis->hSet($order_sum_name,'member_id',$member_id);//砍价者的用户ID
                   $this->redis->hSet($order_sum_name,'headimgurl',$headimgurl);//用户头像
                   $this->redis->hSet($order_sum_name,'nickname',$nickname);//用户昵称
                   $this->redis->hSet($order_sum_name,'ba_order_id',$order_id);//被砍价订单ID
                   $this->redis->hSet($order_sum_name,'in_all',1);//砍价总次数
                   $this->redis->hSet($order_sum_name,'cuont_sunm','0');//砍成功的金额
                   if(!empty($price)){
                       $this->redis->hSet($order_sum_name,'true_all',1);//砍价成功次数
                   }else{
                       $this->redis->hSet($order_sum_name,'true_all',0);//砍价成功次数
                   }
                   $true_count = array();
                   $true_count['member_id'] = $member_id;
                   $true_count['nickname']  = $nickname;
                   $true_count['headimgurl']= $headimgurl;
                   $true_count['in_all'] = 1;
                   $true_count['true_all'] = 1;
                   $true_count['bg_order_id'] = $order_id;
                   $true_count['add_time']    = TIMESTAMP;
                   $true_count['cuont_sunm']  = 0;
                   $datas = $this->table('bargain_count')->where(array('bg_order_id'=>$order_id,'member_id'=>$member_id))->find();
                   if(empty($datas)){
                       $this->table('bargain_count')->insert($true_count);
                   }else{
                       $this->table('bargain_count')->where(array('bg_order_id'=>$order_id,'member_id'=>$member_id))->update($true_count);
                   }
                   $this->redis->hIncrBy($order_name,'in_all',1);//redis 活动订单砍价总次数加一
                   $this->redis->hIncrBy($monthordername,'in_all',1);//当月的活动订单砍价总次数加一
                   $this->redis->zIncrBy($pxname,1,'order_id-'.$order_id);//redis 活动PK榜单当前订单砍赞次数加一
               }

               return 0.00;
           }

        return $order_price;
    }
}